package com.wangsd.api.controller;

import com.wangsd.common.controller.AjaxResult;
import com.wangsd.common.jwt.PassToken;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.common.utils.MyDecimalUtils;
import com.wangsd.member.entity.GoldRecord;
import com.wangsd.member.entity.Member;
import com.wangsd.member.entity.MemberCoupon;
import com.wangsd.member.service.GoldRecordService;
import com.wangsd.member.service.MemberCouponService;
import com.wangsd.member.service.MemberService;
import com.wangsd.system.entity.Trade;
import com.wangsd.system.service.TradeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@Api(tags = "回调管理")
@RestController
@RequestMapping("/api/notify")
@Slf4j
public class NotifyController {
    @Autowired
    private TradeService tradeService;

    /**
     * 汇元支付回调
     * @param map
     * @return
     */
    @PassToken
    @PostMapping("/hyNotify")
    public String hyNotify(@RequestBody Map map){
        log.info("回调回来了："+map);
        String out_trade_no = (String) map.get("out_trade_no");//商户订单号
        String trade_status = (String) map.get("trade_status");//支付状态
        String time_end = (String) map.get("time_end");//支付时间
        Trade trade = tradeService.selectTradeByTradeNo(out_trade_no);
        if (trade != null && trade.getStatus() == 0 && trade_status.equals("Success")) {
            Date date = DateUtils.parseDate(time_end, "yyyyMMddHHmmss");
            String payTime = DateUtils.formatDate(date, "yyyy-MM-dd HH:mm:ss");
            trade.setPayTime(payTime);
            tradeService.updateTradeStatus(trade);
        }
        return "ok";
    }

    /**
     *不付钱修改订单状态
     * @param outTradeNo 订单号
     * @return
     */
    @PassToken
    @PostMapping("/notify")
    public AjaxResult notify(String outTradeNo){
        Trade trade = tradeService.selectTradeByTradeNo(outTradeNo);
        if (trade != null && trade.getStatus() == 0) {
            String payTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
            trade.setPayTime(payTime);
            tradeService.updateTradeStatus(trade);
        }
        return AjaxResult.success("success");
    }
}
